package org.mbds.sensorsdatacollector.android.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.mbds.sensorsdatacollector.android.config.Properties;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class FileUtilities {
	public synchronized static Bitmap loadImage(String fn)
	// load the image stored in fn in images/
	{
		Bitmap bmp = null;
		System.out.println(fn);
		String file = fn;
		bmp = BitmapFactory.decodeFile(file);
		return bmp;
	}
	public static boolean deleteFile(String pathFile) {
		boolean deleted = false;
		try {
	        File datafile = new File(Environment.getExternalStorageDirectory(), pathFile);
	        deleted = datafile.delete();
		} catch (Exception e) {
	    	Log.w("LOG","Failed to dalete file: "+pathFile);
		}
		return deleted;
	}
	public static void saveDataToFile(String pathFile, String data) {
		try{		
			File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
		    if (file.canWrite()){
		        File datafile = new File(file, pathFile);
		        RandomAccessFile raf = new RandomAccessFile(datafile, "rw");
		        if (raf.length()>0)
		        	raf.seek(raf.length()-1);
		        raf.writeBytes(data + "\r\n");
		        raf.close();
		    }
		    else {
		    	Log.w("LOG","Could not write log on the SD card");
		    }
		} catch (IOException e) {
	    	Log.w("LOG","Failed to save log in local file: "+pathFile);
    	}	
	}
	
    public static String getFileContents(final File file) throws IOException {
        final InputStream inputStream = new FileInputStream(file);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        
        final StringBuilder stringBuilder = new StringBuilder();
        
        boolean done = false;
        
        while (!done) {
            final String line = reader.readLine();
            done = (line == null);
            
            if (line != null) {
                stringBuilder.append(line);
            }
        }
        
        reader.close();
        inputStream.close();
        
        return stringBuilder.toString();
    }
	public static void saveLog(String title, String data, int logType) {
    	switch (logType){
    	case 0:
    		Log.d(title,data);
    		break;
    	case 1:
    		Log.i(title,data);
    		break;
    	case 2:
    		Log.e(title,data);
    		break;
    	case 3:
    		Log.w(title,data);
    		break;
    	}
        if (1==1) {

			try{		
				String pathFile=Properties.SD_DIRECTORY+"log.txt";
				File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
			    if (file.canWrite()){
			        File gpxfile = new File(file, pathFile);
			        RandomAccessFile raf = new RandomAccessFile(gpxfile, "rw");
			        if (raf.length()>0)
			        	raf.seek(raf.length()-1);
			        raf.writeBytes(data + "\r\n");
			        raf.close();
			    }
			    else {
			    	Log.w("LOG","Could not write log on the SD card");
			    }
			} catch (IOException e) {
		    	Log.w("LOG","Failed to save log in local file");
	    	}	
        }
	}
	public static InputStream getInputStreamFromUrl(String url) {
	    URL u;
		try {
			u = new URL(url);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
			return null;
		}     
	    HttpURLConnection c;
		try {
			c = (HttpURLConnection) u.openConnection();
		} catch (IOException e1) {
			e1.printStackTrace();
			return null;
		}     
	    try {
			c.setRequestMethod("GET");
		} catch (ProtocolException e) {
			e.printStackTrace();
			return null;
		}     
	    c.setDoOutput(true);     
	    try {
			c.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}     
	    InputStream in;
		try {
			in = c.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}      
	    return in; 
	}
}

